package com.fengplus.demo.controller;

import com.fengplus.demo.entity.AppProduct;
import com.fengplus.demo.entity.AppUser;
import com.fengplus.demo.entity.CommonResult;
import com.fengplus.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/{id}")
    public CommonResult detail(@PathVariable Long id) {
        AppProduct product = productService.findOne(id);
        if(product != null){
            return new CommonResult<>(200, "查询成功", product);
        }else{
            return new CommonResult<>(400, "信息不存在");
        }
    }

    @GetMapping("/{id}/stock")
    public CommonResult stock(@PathVariable Long id) {
        int stock = productService.findStock(id);
        return new CommonResult<>(200, "查询成功", stock);
    }

    @PostMapping
    public CommonResult create(AppProduct product, Integer stock) {
        int isSuccess = productService.save(product, stock);
        if(isSuccess == 1){
            return new CommonResult<>(200, "保存成功");
        }else{
            return new CommonResult<>(400, "保存失败");
        }
    }

}
